<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商户编号" prop="lesseeId">
        <el-input
          v-model="queryParams.lesseeId"
          placeholder="请输入商户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺电话" prop="storePhoto">
        <el-input
          v-model="queryParams.storePhoto"
          placeholder="请输入店铺电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺地址" prop="storeAddr">
        <el-input
          v-model="queryParams.storeAddr"
          placeholder="请输入店铺地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:store:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:store:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:store:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:store:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺编号" align="center" prop="storeId" />
      <el-table-column label="租户编号" align="center" prop="lesseeId" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="店铺电话" align="center" prop="storePhoto" />
      <el-table-column label="店铺地址" align="center" prop="storeAddr" />
      <el-table-column label="门面图" align="center" prop="storePic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.storePic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="店内图" align="center" prop="storeInsidePic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.storeInsidePic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
       <template slot-scope="scope">
         <b v-if="scope.row.status==1" style="color: #1ab394">营业中</b>
         <b v-if="scope.row.status==2" style="color: #C03639">暂停营业</b>
       </template>
      </el-table-column>
      <el-table-column label="宣传视频" align="center" prop="storeVideo" width="100">
        <template slot-scope="scope">
          <video autofocus="true" :src="scope.row.storeVideo" :width="100" :height="50" controls />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:store:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:store:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="openDilagEdit(scope.row)"
          >编辑资料</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门店对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      {{this.form}}
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="租户编号" prop="lesseeId">
          <el-input v-model="form.lesseeId" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="form.storeName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺电话" prop="storePhoto">
          <el-input v-model="form.storePhoto" placeholder="请输入店铺电话" />
        </el-form-item>
        <el-form-item label="店铺地址" prop="storeAddr">
          <el-input v-model="form.storeAddr" placeholder="请输入店铺地址" />
        </el-form-item>
        <el-form-item label="门面图" prop="storePic">
          <image-upload v-model="form.storePic"/>
        </el-form-item>
        <el-form-item label="店内图" prop="storeInsidePic">
          <image-upload v-model="form.storeInsidePic"/>
        </el-form-item>
<!--        <el-form-item label="宣传视频" prop="storeVideo">-->
<!--          <img-upload v-model="form.storeVideo"/>-->
        宣传视频
          <el-upload
            class="upload-demo"
            drag
            action="http://localhost:8080/store/store/upload"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
<!--        </el-form-item>-->

<!--        <el-divider content-position="center">租户信息</el-divider>-->
<!--        <el-row :gutter="10" class="mb8">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbLessee">添加</el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbLessee">删除</el-button>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-table :data="tbLesseeList" :row-class-name="rowTbLesseeIndex" @selection-change="handleTbLesseeSelectionChange" ref="tbLessee">-->
<!--          <el-table-column type="selection" width="50" align="center" />-->
<!--          <el-table-column label="序号" align="center" prop="index" width="50"/>-->
<!--          <el-table-column label="商户名称" prop="lesseeName" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.lesseeName" placeholder="请输入商户名称" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="法人名称" prop="signName" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.signName" placeholder="请输入法人名称" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="法人身份证号" prop="identityCard" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.identityCard" placeholder="请输入法人身份证号" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="联系方式" prop="lesseePhone" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.lesseePhone" placeholder="请输入联系方式" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="注册地址" prop="lesseeAddr" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.lesseeAddr" placeholder="请输入注册地址" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="付费模式" prop="lesseePolicy" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.lesseePolicy" placeholder="请输入付费模式" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="审批通过时间" prop="approvalTime" width="240">-->
<!--            <template slot-scope="scope">-->
<!--              <el-date-picker clearable v-model="scope.row.approvalTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择审批通过时间" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="到期时间" prop="endTime" width="240">-->
<!--            <template slot-scope="scope">-->
<!--              <el-date-picker clearable v-model="scope.row.endTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择到期时间" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="状态" prop="state" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <el-input v-model="scope.row.state" placeholder="请输入状态" />-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--        </el-table>-->
<!--      -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    编辑资料 1 pass -->
    <el-dialog title="完善租户门店资料" :visible.sync="dialogFormVisible">
        <el-steps :space="200" :active="active" finish-status="success">
          <el-step title="租户身份查验"></el-step>
          <el-step title="门店信息"></el-step>
          <el-step title="付费模式选择"></el-step>
          <el-step title="宣传视频"></el-step>
        </el-steps>

<!--      编号0的租户身份查验-->
      <el-form v-show="active==0" ref="form" :model="DataForm" :rules="rules" label-width="80px">
        <el-form-item label="资料编号" prop="dataId">
          <el-input v-model="DataForm.dataId" placeholder="请输入资料编号" />
        </el-form-item>
        <el-form-item label="租户编号" prop="lesseeId">
          <el-input v-model="DataForm.lesseeId" placeholder="请输入租户编号" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="DataForm.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="组织结构编码" prop="organizationCode">
          <el-input v-model="DataForm.organizationCode" placeholder="请输入组织结构编码" />
        </el-form-item>
        <el-form-item label="身份证正面照" prop="idIs">
          <image-upload v-model="DataForm.idIs"/>
        </el-form-item>
        <el-form-item label="身份证反面照" prop="idThe">
          <image-upload v-model="DataForm.idThe"/>
        </el-form-item>
        <el-form-item label="法人姓名" prop="personName">
          <el-input v-model="DataForm.personName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="经营范围" prop="businessScope">
          <el-input v-model="DataForm.businessScope" placeholder="请输入经营范围" />
        </el-form-item>
        <el-form-item label="营业执照图片" prop="businessLicense">
          <image-upload v-model="DataForm.businessLicense"/>
        </el-form-item>
      </el-form>
<!--      编号1的租户信息-->
      <el-form v-show="active==1" ref="form" :model="StoreForm" :rules="rules" label-width="80px">
        <el-form-item label="租户编号" prop="lesseeId">
          <el-input v-model="StoreForm.lesseeId" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="StoreForm.storeName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺电话" prop="storePhoto">
          <el-input v-model="StoreForm.storePhoto" placeholder="请输入店铺电话" />
        </el-form-item>
        <el-form-item label="店铺地址" prop="storeAddr">
          <el-input v-model="StoreForm.storeAddr" placeholder="请输入店铺地址" />
        </el-form-item>
        <el-form-item label="门面图" prop="storePic">
          <image-upload v-model="StoreForm.storePic"/>
        </el-form-item>
        <el-form-item label="店内图" prop="storeInsidePic">
          <image-upload v-model="StoreForm.storeInsidePic"/>
        </el-form-item>
      </el-form>

        <el-button style="margin-top: 12px;" @click="next">下一步</el-button>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>

    <!--    编辑资料-->
    <el-dialog title="完善租户门店资料" :visible.sync="dialogFormVisible">
      <el-steps :space="200" :active="active" finish-status="success">
        <el-step title="租户身份查验"></el-step>
        <el-step title="门店信息"></el-step>
        <el-step title="付费模式选择"></el-step>
        <el-step title="宣传视频"></el-step>
      </el-steps>

      <!--      编号0的租户身份查验-->
      <el-form v-show="active==0" ref="form" :model="DataForm" :rules="rules" label-width="80px">
        <el-form-item label="租户编号" prop="lesseeId">
          <el-input v-model="DataForm.lesseeId" placeholder="请输入租户编号" />
        </el-form-item>

        <el-form-item label="身份证正面照" prop="idIs">
          <image-upload v-model="DataForm.idIs"/>
        </el-form-item>
        <el-form-item label="身份证反面照" prop="idThe">
          <image-upload v-model="DataForm.idThe"/>
        </el-form-item>

      </el-form>
      <!--      编号1的租户信息-->
      <el-form v-show="active==1" ref="form" :model="StoreForm" :rules="rules" label-width="80px">
        <el-form-item label="租户编号" prop="lesseeId">
          <el-input v-model="StoreForm.lesseeId" placeholder="请输入商户编号" />
        </el-form-item>

        <el-form-item label="门面图" prop="storePic">
          <image-upload v-model="StoreForm.storePic"/>
        </el-form-item>
        <el-form-item label="店内图" prop="storeInsidePic">
          <image-upload v-model="StoreForm.storeInsidePic"/>
        </el-form-item>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>

      </el-form>

      <el-button style="margin-top: 12px;" @click="next">下一步</el-button>

    </el-dialog>


  </div>
</template>

<script>
import { listStore, getStore, delStore, addStore, updateStore } from "@/api/store/store";
import {getToken} from "@/utils/auth";

export default {
  name: "Store",
  data() {
    return {
      headers:{
        Authorization:''
      },
      dialogFormVisible:false,
      active: 0,
      StoreForm:{},//门店对象
      DataForm:{},//资料卡对象

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbLessee: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      // 租户表格数据
      tbLesseeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lesseeId: null,
        storeName: null,
        storePhoto: null,
        storeAddr: null,
        storePic: null,
        storeInsidePic: null,
        status: null,
        storeVideo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    //获取token
    this.headers.Authorization="Bearer "+getToken();
    console.log(this.headers.Authorization)

    this.getList();
  },
  methods: {
    //上传视频
    handleAvatarSuccess(res, file) {
      // if (res){
        console.log(res.data)
        this.form.storeVideo=res
      // }else{
      //   alert("上传失败")
      // }
    },

    //步骤条函数
    next() {
      if (this.active++ > 2) this.active = 0;
    },
    //编辑资料函数
    openDilagEdit(row){
      this.dialogFormVisible=true;
      this.StoreForm=row;

    },
    /** 查询门店列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        storeId: null,
        lesseeId: null,
        storeName: null,
        storePhoto: null,
        storeAddr: null,
        storePic: null,
        storeInsidePic: null,
        status: null,
        storeVideo: null
      };
      this.tbLesseeList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加门店";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const storeId = row.storeId || this.ids
      getStore(storeId).then(response => {
        this.form = response.data;
        this.tbLesseeList = response.data.tbLesseeList;
        this.open = true;
        this.title = "修改门店";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbLesseeList = this.tbLesseeList;
          if (this.form.storeId != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeIds = row.storeId || this.ids;
      this.$modal.confirm('是否确认删除门店编号为"' + storeIds + '"的数据项？').then(function() {
        return delStore(storeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 租户序号 */
    rowTbLesseeIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 租户添加按钮操作 */
    handleAddTbLessee() {
      let obj = {};
      obj.lesseeName = "";
      obj.signName = "";
      obj.identityCard = "";
      obj.lesseePhone = "";
      obj.lesseeAddr = "";
      obj.lesseePolicy = "";
      obj.approvalTime = "";
      obj.endTime = "";
      obj.state = "";
      this.tbLesseeList.push(obj);
    },
    /** 租户删除按钮操作 */
    handleDeleteTbLessee() {
      if (this.checkedTbLessee.length == 0) {
        this.$modal.msgError("请先选择要删除的租户数据");
      } else {
        const tbLesseeList = this.tbLesseeList;
        const checkedTbLessee = this.checkedTbLessee;
        this.tbLesseeList = tbLesseeList.filter(function(item) {
          return checkedTbLessee.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbLesseeSelectionChange(selection) {
      this.checkedTbLessee = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/store/export', {
        ...this.queryParams
      }, `store_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
